/**************************
	Maxthon Library
	By Neo101
	Version 1.1.0
	Chang by pharaoh
**************************/


var MaxLib = {

	SETTINGS_FILE : 'Settings.ini',
	DEFAULT_SETTINGS_FILE : 'DefaultSettings.ini',
	INI_SECTION : 'settings',
	M1_DATA_FOLDER : 'data/',
	M2_DATA_FOLDER : null,


	showScriptError : function(message, url, line){
		var d = new Date();
		var date = MaxLib.timeToString( d.getHours() )+':'+MaxLib.timeToString( d.getMinutes() )+':'+MaxLib.timeToString( d.getSeconds() );
		var errorMessage = 'Message: '+message+' Line: '+line+' URL: '+url+' Date: '+date;
		external.get_tab(SECURITY_ID, external.cur_sel).window.status = errorMessage;
	},

	
	/**********************   for INI files   *****************************/
	
	writeSetting : function(key, data) {
		this.writeIni(this.SETTINGS_FILE, this.INI_SECTION, key, data+'');
	},
	
	
	readSetting : function(key, errorResponse) {
		if(errorResponse == null){
			errorResponse = '';
		}
		var returnValue = this.readIni(this.SETTINGS_FILE, this.INI_SECTION, key, 'error');
		if(returnValue == 'error') {
			var defaultFileValue = this.readIni(this.DEFAULT_SETTINGS_FILE, this.INI_SECTION, key, 'error');
			if(defaultFileValue == 'error'){
				return errorResponse;
			}
			else {
				this.writeSetting(key, defaultFileValue);
				returnValue = defaultFileValue;
			}
		}
		
		return returnValue;
	},


	checkSettingsFile: function(){
		if( this.readFile(this.SETTINGS_FILE) != undefined && this.readFile(this.SETTINGS_FILE) != ''){
			return false;
		}
		var defaultSettingsFileData = this.readFile(this.DEFAULT_SETTINGS_FILE);
		if(defaultSettingsFileData && defaultSettingsFileData != ''){
			this.writeFile(this.SETTINGS_FILE, defaultSettingsFileData);
		}
		return true;
	},


	readIni : function(file, section, key, errorResponse){
		if(errorResponse == null){
			errorResponse = '';
		}
		return external.m2_readIni(SECURITY_ID, PLUGIN_NAME, (this.M2_DATA_FOLDER || this.M1_DATA_FOLDER) + file, section, key, errorResponse);
	},


	writeIni : function(file, section, key, data) {
		external.m2_writeIni(SECURITY_ID, PLUGIN_NAME, (this.M2_DATA_FOLDER || this.M1_DATA_FOLDER) + file, section, key, data+'');
	},
	
	
	/**********************   for other files files   *****************************/
	
	readFile : function(filename, folder) {
		return external.readFile(SECURITY_ID, PLUGIN_NAME, (folder ? folder + '/' : (this.M2_DATA_FOLDER || this.M1_DATA_FOLDER)) + filename);
	},


	writeFile : function(filename, data, folder) {
		external.writeFile(SECURITY_ID, PLUGIN_NAME, (folder ? folder : (this.M2_DATA_FOLDER || this.M1_DATA_FOLDER)) + filename, data);
	},
	
	
	/**********************   for Maxthon 2.x   *****************************/
	
	getM2DataPath : function(){
		try{
			var infoObj = external.max_getObj(SECURITY_ID, 'info');
			if(infoObj){
				return infoObj.getFolderPluginData(PLUGIN_NAME);
			} else {
				return null;
			}
		} catch(e) {
			return null;
		}
	},
	
	/********************* Add By pharaoh **************************/
	ActiveXObject:function(progID) {
		return external.max_activex(SECURITY_ID,progID);
	}
	
};


MaxLib.M2_DATA_FOLDER = MaxLib.getM2DataPath();

//	alert(MaxLib.M2_DATA_FOLDER);


window.onerror = MaxLib.showScriptError;
function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, "");}
function clear0(str) {return str.replace(/\0/g,'');}